<?php
// @formatter:off
/**
 * @file WCSearchFactory.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-18 22:13:05 +0000 (Thu, 18 Jul 2013) $
 *
 * $Id: WCSearchFactory.php 97 2013-07-18 22:13:05Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/includes/search/WCSearchFactory.php $
 */
// @formatter:on

require_once dirname(__FILE__)."/WCSearchItems.php";

/**
 * @class WCSearchFactory This factory is a collection of function used by the
 * search engine.
 */
class WCSearchFactory {
	// @formatter:off
	/**
	 * @var int[string] This is the list of priorities of each item type when
	 * they are searched.
	 */
	public static $TypePriority = array(
			"CL"	=> 1,	// 4,
			"TA"	=> 1,	// 3,
			"CX"	=> 1,	// 2,
			"DI"	=> 1,	// 1,
			"RL"	=> 1	// 1,
	);
	// @formatter:on
	/**
	 * @var array This is the list of character considered to be words
	 * separator.
	 */
	protected static $_Separators = array(
		"-",
		"/",
		".",
		"[",
		"]",
		"(",
		")",
		"_",
		"&",
		"+",
		"~",
		"?",
		"°",
		"¿",
		"¡",
		"!",
		"'",
		'"',
		":"
	);
	/**
	 * Since this is just a factory, its constructor is protected to avoid
	 * instantiations of this class.
	 */
	protected function __construct() {
	}
	/**
	 * Prevent users to clone the instance.
	 */
	public function __clone() {
		trigger_error(__CLASS__.": Clone is not allowed.", E_USER_ERROR);
	}
	//
	// Public class methods.
	/**
	 * This factory method creates a search item based on its type. This
	 * method only requires element called "type" in the information array to
	 * build an object, but more elements may be required by WCSearchItem
	 * inherited classes (Visit its definition for more information about).
	 *
	 * @param WCGroup $group Group associated with the item to create.
	 * @param mixed[string] $info Information associative array.
	 * @param string $hash ID required to identify an item.
	 * @return WCSearchItem Returns the requested item.
	 */
	public static function BuildItem(WCGroup &$group, $info = array(), $hash = false) {
		//
		// Checking information structure.
		if(!isset($info["type"])) {
			//
			// The information structure did not specify a item type.
			trigger_error(__CLASS__."::".__FUNCTION__."(): Wrong item information.", E_USER_ERROR);
		}
		//
		// Building item representing class name.
		$class = "WCSearchItem{$info["type"]}";
		//
		// Checking class existence
		if(!class_exists($class)) {
			//
			// Required class does not exist.
			trigger_error(__CLASS__."::".__FUNCTION__."(): Class '{$class}' doesn't exist.", E_USER_ERROR);
		}
		//
		// Creating and returning a search item.
		return new $class($group, $info, $hash);
	}
	/**
	 * This method generates a hash ID to represent search items.
	 *
	 * @param mixed $sitRow Row obtained from table '%search_items'.
	 * @return string Returns a hash ID.
	 */
	public static function SearchItemHash(&$sitRow) {
		//
		// Hash must include group ID and item type.
		$id = $sitRow["sit_group"].$sitRow["sit_type"];
		//
		// Also, it must include the item's ID.
		// @{
		switch($sitRow["sit_type"]) {
			case "CL":
				$id .= $sitRow["sit_collection"];
				break;
			case "TA":
				$id .= $sitRow["sit_tag"];
				break;
			case "CX":
				$id .= $sitRow["sit_comic"];
				break;
			case "DI":
				$id .= $sitRow["sit_directory"];
				break;
			case "RL":
				$id .= $sitRow["sit_rule"];
				break;
		}
		// @}
		//
		// Returning built ID.
		return $id;
	}
	/**
	 * This method allows to take a string and convert it into a list
	 * searchable words.
	 *
	 * @param string String to be converted int a pattern.
	 * @return string[] Returns a list of searchable words.
	 */
	public static function StringToPatterns($str) {
		//
		// Cleaning separators.
		$str = strtolower(str_replace(WCSearchFactory::$_Separators, " ", $str));
		//
		// Expanding the string into a list of words and clean empty
		// words.
		$str = array_filter(explode(" ", $str), "WCSearchFactory::WordsFilter");
		//
		// Restarting indexing.
		$str = array_values($str);
		//
		// Returning the list of words.
		return $str;
	}
	/**
	 * This is an evaluation function that detects empty words.
	 *
	 * @param string $value Word to be analyzed.
	 * @return boolean Returns true when the word is not empty.
	 */
	public static function WordsFilter($value) {
		return $value != "";
	}
}
?>
